package com.rtos.vo.file.diskPane;


import com.rtos.vo.file.blockTableView.BlockTableView;
import com.rtos.model.fileSystem.FileUtil;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class DiskViewer {
    public Pane diskPane;
    public BlockPane[] blocks;
    public Rectangle rectangle;
    public int columnNumber = 15;
    public BlockTableView tableView;
    public int choosenBlockIndex;
    public static final int START_X = 6;
    public static final int START_Y = 6;

    public DiskViewer(Pane diskPane) {
        this.diskPane = diskPane;
        this.blocks = new BlockPane[128];
        this.choosenBlockIndex = 0;
        this.initializeBlockPanes(this.blocks);
        this.updateStatus();
    }
    public void setUsed(){
        this.rectangle.setFill(Color.GREEN);
    }
    public void initializeBlockPanes(BlockPane[] blocks) {
        EventHandler<MouseEvent> listener = new EventHandler<MouseEvent>() {
            public void handle(MouseEvent event) {
                DiskViewer.this.choosenBlockIndex = ((BlockPane)event.getSource()).getIndex();
//                鼠标点击事件变绿
//                blocks[DiskViewer.this.choosenBlockIndex].rectangle.setFill(Color.GREEN);
//                DiskViewer.this.tableView.setBlockBytes(FileSystemKernel.getInstance().getBlockBytes(DiskViewer.this.choosenBlockIndex, 0, 64));
            }
        };
        for(int i = 0; i < blocks.length / this.columnNumber; ++i) {
            for(int j = 0; j < this.columnNumber; ++j) {
                int index = i * this.columnNumber + j;
                if (blocks[index] == null) {
                    blocks[index] = new BlockPane(index);
                    blocks[index].setLayoutX((double)(20 + j * 35));
                    blocks[index].setLayoutY((double)(5 + i * 30));
                    //blocks[index].setOnMouseClicked(listener);
                    this.diskPane.getChildren().add(blocks[index]);
                }
            }
        }

    }

    public void setBlockTableView(BlockTableView tableView) {
        this.tableView = tableView;
    }

    public void updateStatus() {
        FileUtil fileUtil = null;
        try {
            fileUtil = FileUtil.getInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        int[] fat = fileUtil.getFAT();

        for(int i = 0; i < fat.length; ++i) {
            switch(fat[i]) {
                case -2:
                    this.blocks[i].setDamage();
                    break;
                case 0:
//                    this.blocks[i].setUnUsed();
                    break;
                default:
                    this.blocks[i].setUsed();
            }
        }

    }

//    public void update() {
//        this.updateStatus();
////        this.tableView.setBlockBytes(FileSystemKernel.getInstance().getBlockBytes(this.choosenBlockIndex, 0, 64));
////        System.out.println("DiskViewer 更新成功");
//    }
}


